//
//  LWQrcodeViewController.m
//  1.28  weibo
//
//  Created by mac on 15/1/31.
//  Copyright (c) 2015年 mac. All rights reserved.
//

#import "LWQrcodeViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface LWQrcodeViewController () <AVCaptureMetadataOutputObjectsDelegate>
- (IBAction)closeClick:(id)sender;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topConstraint;
@property (nonatomic,strong) CADisplayLink *link;
/**
 *  预览界面
 */
@property (nonatomic,strong) AVCaptureVideoPreviewLayer *previewLayer;
/**
 *  会话
 */
@property (nonatomic,strong) AVCaptureSession *session;
@end

@implementation LWQrcodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];

   
}
    /**
     *  什么是输入设备
     * 话筒和相机...
*/
    /*
#pragma mark - 回去输入设备一定要用default的方法,不能直接alloc init
   //1.获取输入设备
    AVCaptureDevice *inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    //2. 创建输入对象
    AVCaptureDeviceInput *input = [[AVCaptureDeviceInput alloc] initWithDevice:inputDevice error:NULL];
    //3. 创建输出对象
    AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
    //4.设置输出设备的代理
    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    //5.创建会话
    AVCaptureSession *session = [[AVCaptureSession alloc] init];
    self.session = session;
    //6.将输入和输出对象添加到会话
    if ([session canAddInput:input]) {
        [session canAddInput:input];
    }
    if ([session canAddOutput:output]) {
        [session canAddOutput:output];
    }
    //7.设置输出的类型
    [output setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
    //8.设置预览的界面
    AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];
    previewLayer.frame = self.view.bounds;
    [self.view.layer insertSublayer:previewLayer above:0];
    self.previewLayer = previewLayer;
    //9.开始会话
    [session startRunning];
    
}



#pragma mark - 代理的方法
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{

    if (metadataObjects.count > 0) {
        [self.session startRunning];
        [self.previewLayer removeFromSuperlayer];
        AVMetadataMachineReadableCodeObject *obj = [metadataObjects lastObject];
        [self.link invalidate];
        self.link = nil;
        UILabel *lable = [[UILabel alloc] initWithFrame:self.view.bounds];
        lable.numberOfLines = 0;
        lable.text = obj.stringValue;
        [self.view addSubview:lable];
    }
    
}
*/

#pragma mark - 定时器监听的方法

//移除tabbar中的横线
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    for (UIView *child in self.tabBarController.tabBar.subviews) {
        if ([child isKindOfClass:[UIImageView class]]) {
            [child removeFromSuperview];
        }
    }
    //设置定时器
    CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(upData)];
    [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
    self.link = link;
}

- (void)upData{
    
    self.topConstraint.constant -= 5;
    if (self.topConstraint.constant <= -170) {
        self.topConstraint.constant = 170;
    }
}

- (void)viewWillDisappear:(BOOL)animated{

    [super viewWillDisappear:animated];
    [self.link invalidate];
    self.link = nil;
    
}
- (IBAction)closeClick:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
